﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace Demo.Common
{
    /// <summary>
    /// GetTreeNodes 的摘要说明
    /// </summary>
    public class GetTreeNodes : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string parentId = context.Request["id"];
            if (string.IsNullOrEmpty(parentId))
                parentId = "0";

            Demo.Data.MatrixData data = new Data.MatrixData("datuhe");
            var dataTable = data.FetchDataTable(PetaPoco.Sql.Builder
                .Append("select * from 基础_物料分类")
                .Where("父物料分类ID=@0", parentId)
                .OrderBy("排序号")
                );

            List<ExtJSExtender.Controls.TreeNode> nodes = new List<ExtJSExtender.Controls.TreeNode>();
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                foreach (DataRow row in dataTable.Rows)
                {
                    var tmpNode = new ExtJSExtender.Controls.TreeNode()
                    {
                        id = row["物料分类ID"].ToString(),
                        text = row["物料分类名称"].ToString(),
                        expanded = false
                    };
                    if (row["是否叶节点"].ToString() == "是")
                        tmpNode.leaf = true;

                    nodes.Add(tmpNode);
                }
            }

            context.Response.ContentType = "text/json";
            context.Response.Write(ExtJSExtender.Util.SerializeToJson(nodes));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}